{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.2685457  -0.04424934 -0.61183471]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "#输入数据\n",
    "X=np.array([[1,3,3],\n",
    "           [1,4,3],\n",
    "           [1,1,1]])\n",
    "\n",
    "#标签\n",
    "Y=np.array([1,1,-1])\n",
    "#权值初始化，1行3列，取值范围-1到1\n",
    "W=(np.random.random(3)-0.5)*2\n",
    "print(W)\n",
    "#学习率设置\n",
    "lr=0.11\n",
    "#计算迭代次数\n",
    "n=0\n",
    "#神经网络输出\n",
    "O=0\n",
    "\n",
    "def update():\n",
    "    global X,Y,W,lr,n\n",
    "    n+=1\n",
    "    O=np.sign(np.dot(X,W.T))\n",
    "    w_c=lr*((Y-O.T).dot(X))/int(X.shape[0])\n",
    "    W=W+w_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.41521237  0.469084   -0.17183471]\n",
      "1\n",
      "[ 0.34187904  0.39575066 -0.24516804]\n",
      "2\n",
      "[ 0.2685457   0.32241733 -0.31850137]\n",
      "3\n",
      "[ 0.19521237  0.249084   -0.39183471]\n",
      "4\n",
      "[ 0.19521237  0.39575066 -0.24516804]\n",
      "5\n",
      "[ 0.12187904  0.32241733 -0.31850137]\n",
      "6\n",
      "[ 0.0485457   0.249084   -0.39183471]\n",
      "7\n",
      "[0.19521237 0.76241733 0.04816529]\n",
      "8\n",
      "[ 0.12187904  0.689084   -0.02516804]\n",
      "9\n",
      "[ 0.0485457   0.61575066 -0.09850137]\n",
      "10\n",
      "[-0.02478763  0.54241733 -0.17183471]\n",
      "11\n",
      "[-0.09812096  0.469084   -0.24516804]\n",
      "12\n",
      "[-0.1714543   0.39575066 -0.31850137]\n",
      "13\n",
      "finished\n",
      "epoch: 13\n",
      "k= 1.2425398950374407\n",
      "d= -0.5383157245720915\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD9CAYAAACcJ53WAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4xLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvDW2N/gAAG+JJREFUeJzt3XmUldWVxuHflqgRJMEoohEFNQZBBJQKDqhtRA0OS9NpY2tjEodYiVOcug3dRluN2gYn1EawAEdQFJkFQURQkbGKGUscaARFBRyCCIJQu//YFUegLnC/+93hfdZi1ZBb1HtXltvt+c7Zx9wdEREpHNulHUBERLaMCreISIFR4RYRKTAq3CIiBUaFW0SkwKhwi4gUmIwKt5k1MrOnzew1M6s2syOSDiYiIhv3vQxfdw8w2t3PMLMdgPoJZhIRkc2wug7gmNkPgNnAfq7TOiIiqctkqWQ/YDnwkJnNNLM+ZtYg4VwiIrIJmXTcZcAUoKO7TzWze4CV7n7dt15XDpQDNGjQoP2BBx6YUGQRkeJTVVW1wt0bZ/LaTAr3HsAUd29e+/XRQFd3P2VTP1NWVuaVlZWZJxYRKXFmVuXuZZm8ts6lEnd/H1hiZi1qv9UJeHUb8omIyDbIdFfJZUD/2h0lC4HzkoskIiKbk1HhdvdZQEYtvIiIJEsnJ0VECowKt4hIgVHhFhEpMCrcIiIFRoVbRCQbJk6Ebt1y8qtUuEVEtsWnn8Kll8LRR8MDD8BnnyX+K1W4RUS21ujR0Lo13H8/XH45zJ4NDZIf5aTCLSKypT78EH73OzjppCjUr7wC3bvDzjvn5NercIuIZModBg6EVq3g8cfhL3+BmTPhiNzeLZPpkXcRkdL23ntw8cUwdCi0bw/PPQdt26YSRR23iMjmuMODD0LLlrGm3a0bTJmSWtEGddwiIpv2f/8H5eXw/PNwzDHQuzf89Kdpp1LHLSLy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},
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "for _ in range(100):\n",
    "    update()#跟新权值\n",
    "    print(W)#打印当前权值\n",
    "    print(n)#打印迭代次数\n",
    "    O=np.sign(np.dot(X,W.T))#计算当前输出\n",
    "    if(O==Y.T).all():#如果实际输出等于期望输出，模型收敛，循环结束\n",
    "        print('finished')\n",
    "        print('epoch:',n)\n",
    "        break\n",
    "\n",
    "#正样本\n",
    "x1=[3,4]\n",
    "y1=[3,3]\n",
    "#负样本\n",
    "x2=[1]\n",
    "y2=[1]\n",
    "\n",
    "#计算分界线斜率以及截距\n",
    "k=-W[1]/W[2]\n",
    "d=-W[0]/W[2]\n",
    "print('k=',k)\n",
    "print('d=',d)\n",
    "\n",
    "xdata = np.linspace(0,5)\n",
    "plt.figure()\n",
    "plt.plot(xdata,xdata*k+d,'r')\n",
    "plt.plot(x1,y1,'bo')\n",
    "plt.plot(x2,y2,'yo')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
